package com.gandong8.hbase.web.controller;

import com.gandong8.hbase.web.domain.Result;
import com.gandong8.hbase.web.exception.LoginException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Controller
public class LoginController {
    @Value("${login.username}")
    private String loginUsername;
    @Value("${login.password}")
    private String loginPassword;

    @RequestMapping(value = "/login")
    @ResponseBody
    public ModelAndView login() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("pageTitle", "表管理");
        modelAndView.setViewName("login");
        return modelAndView;
    }

    @RequestMapping(value = "/login/out")
    @ResponseBody
    public String loginOut(HttpServletRequest request) {
        request.getSession().removeAttribute("login_user");
        throw new LoginException("成功登出");
    }


    @RequestMapping(value = "/login/do")
    @ResponseBody
    public Result loginDo(HttpServletRequest request,String username, String password) {
        if(loginUsername.equals(username) && loginPassword.equals(password)) {
            request.getSession().setAttribute("login_user", username);
            return Result.success();
        }
        return Result.failed();
    }
}
